﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.Components.Dimensions;

namespace ITAP.ContourBuilder.Components.Constraints
{
    [Serializable]
    public class ConstraintRemoveInvalidDimension : Constraint, IDimensionVisitor
    {
        public override bool Check(IContourComponent component)
        {
            return false;
        }

        public override void Fix(IContourComponent component)
        {
            if (!enabled)
                return;
            if(!(component is Dimension))
                return;

            (component as Dimension).AcceptDimensionVisitor(this);
        }

        private void DefaultRemove(Dimension dimension)
        {
            if (dimension.ConnectedComponents.GetCount<IContourComponent>() == 0)
                dimension.Parent.RemoveInnerComponent(dimension);
        }

        #region IDimensionVisitor Members

        public void Visit(ContourElementDimension contourElementDimension)
        {
            this.DefaultRemove(contourElementDimension);
        }

        public void Visit(ElementLeaderDimension leaderDimension)
        {
            this.DefaultRemove(leaderDimension);            
        }

        public void Visit(ArcHeightDimension arcHeightDimension)
        {
            this.DefaultRemove(arcHeightDimension);
        }

        public void Visit(ArcRadiusDimension2 arcRadiusDimension)
        {
            this.DefaultRemove(arcRadiusDimension);
        }

        public void Visit(ComponentLeaderDimension leaderDimension)
        {
            if (leaderDimension.ConnectedComponents.Count<IContourComponent>() != 2)
                leaderDimension.Parent.RemoveInnerComponent(leaderDimension);
        }

        public void Visit(DiameterDimension diameterDimension)
        {
            this.DefaultRemove(diameterDimension);
        }

        public void Visit(RemoteRadiusDimension radiusDimension)
        {
            this.DefaultRemove(radiusDimension);
        }

        public void Visit(DirectDistanceDimension distanceDimension)
        {
            if (distanceDimension.ConnectedComponents.Count<IContourComponent>() != 2)
                distanceDimension.Parent.RemoveInnerComponent(distanceDimension);
        }

        public void Visit(OffsetFromMainBorderDimension offsetDimension)
        {            
        }

        #endregion
    }
}
